package com.aaa.market.controller;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.market.entity.Advertise;
import com.aaa.market.entity.CouponHistory;
import com.aaa.market.service.CouponHistoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (CouponHistory)表控制层
 *
 * @author makejava
 * @since 2020-11-17 19:14:44
 */
@RestController
@RequestMapping("couponHistory")
public class CouponHistoryController {
    /**
     * 服务对象
     */
    @Resource
    private CouponHistoryService couponHistoryService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public CouponHistory selectOne(Long id) {
        return this.couponHistoryService.queryById(id);
    }


    /**
     * 新增
     */
    @PostMapping("insertCouponHistory")
    public Result insertCouponHistory(@RequestBody CouponHistory couponHistory){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),couponHistoryService.insert(couponHistory));
    }


    /**
     * 修改
     */
    @PutMapping("updateCouponHistory")
    public Result updateCouponHistory(@RequestBody CouponHistory couponHistory){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),couponHistoryService.update(couponHistory));
    }


    @DeleteMapping("deleteCouponHistory")
    public Result deleteCouponHistory(Long id){
        //当ID为空时抛出自定义异常
        if (null==id){
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.couponHistoryService.deleteById(id));
    }

    /**
     * 根据相关条件查询下单用户可用优惠券
     * @param map
     * @return
     */
    @PostMapping("queryAllByParam")
    public Result queryAllByParam(@RequestBody Map map) {
        return couponHistoryService.queryAllByParam(map);
    }

    /**
     *
     * 查询客户优惠券
     * @return
     */
    @GetMapping("selectAll")
   public Result selectAll(Integer pageNo,Integer pageSize,CouponHistory couponHistory){
       return couponHistoryService.selectAll(pageNo,pageSize,couponHistory);
   };

}